package com.ls.kdlss.ui.home;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ls.kdlss.R;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/5.
 */

public class MyFilter extends PopupWindow {

    private Context context;
    private RecyclerView rcyGrade;
    private RecyclerView rcySubject;


    private static List<String> grade;
    private static List<String> subject1;
    private static List<String> subject2;
    private static List<String> subject3;
    private static int gradePosition;
    private static int subjectPosition;
    private OnChoose choose;

    static {

        grade = new ArrayList<>();
        grade.add("全部");
        grade.add("小学");
        grade.add("初一");
        grade.add("初二");
        grade.add("初三");
        grade.add("高一");
        grade.add("高二");
        grade.add("高三");
    }

    static {
        subject1 = new ArrayList<>();
        subject1.add("全部");
        subject1.add("语文");
        subject1.add("数学");
        subject1.add("英语");
        subject1.add("生物");
        subject1.add("政治");
        subject1.add("历史");
        subject1.add("地理");
        subject1.add("物理");
        subject1.add("化学");
    }


    static {
        subject2 = new ArrayList<>();
        subject2.add("全部");
        subject2.add("语文");
        subject2.add("数学");
        subject2.add("英语");
        subject2.add("生物");
        subject2.add("政治");
        subject2.add("历史");
        subject2.add("地理");
        subject2.add("物理");
    }


    static {
        subject3 = new ArrayList<>();
        subject3.add("全部");
        subject3.add("语文");
        subject3.add("数学");
        subject3.add("英语");

    }


    public MyFilter(Context context) {
        super(context);
        this.context = context;
        initView(context);
    }

    private void initView(final Context context) {
        setPopup();
        setGrade();
    }


    /**
     * 初始化 popup
     */
    private void setPopup() {
        View view = LayoutInflater.from(context).inflate(R.layout.cell_filter, null);
        setContentView(view);
        rcyGrade = (RecyclerView) view.findViewById(R.id.rcy_grade);
        rcySubject = (RecyclerView) view.findViewById(R.id.rcy_subject);
        LinearLayoutManager manager1 = new LinearLayoutManager(context);
        LinearLayoutManager manager2 = new LinearLayoutManager(context);
        rcyGrade.setLayoutManager(manager1);
        rcySubject.setLayoutManager(manager2);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);

    }


    /**
     * 设置年级列表
     */
    private void setGrade() {
        rcyGrade.setAdapter(new CommonAdapter<String>(context, R.layout.cell_grade, grade) {
            @Override
            protected void convert(final ViewHolder holder, String s, final int position) {
                if (gradePosition == position) {
                    holder.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                    ((TextView) holder.getView(R.id.tv_grade)).setTextColor(context.getResources().getColor(R.color.tv_tab_select_text));
                    switch (gradePosition) {
                        case 1:
                            setProject(2);
                            break;
                        case 2:
                            setProject(1);
                            break;
                        case 3:
                            setProject(1);
                            break;
                        default:
                            setProject(0);
                            break;

                    }
                }
                holder.setText(R.id.tv_grade, s);
                holder.getView(R.id.tv_grade).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        for (int i = 0; i < getItemCount(); i++) {
                            View view = rcyGrade.getChildAt(i);
                            if (null != rcyGrade.getChildViewHolder(view)) {
                                ViewHolder mHolder = (ViewHolder) rcyGrade.getChildViewHolder(view);
                                mHolder.getView(R.id.iv_status).setVisibility(View.GONE);
                                ((TextView) mHolder.getView(R.id.tv_grade)).setTextColor(context.getResources().getColor(R.color.black));
                            }
                        }
                        holder.getView(R.id.iv_status).setVisibility(View.VISIBLE);
                        ((TextView) holder.getView(R.id.tv_grade)).setTextColor(context.getResources().getColor(R.color.tv_tab_select_text));
                        switch (position) {
                            case 1:
                                setProject(2);
                                break;
                            case 2:
                                setProject(1);
                                break;
                            case 3:
                                setProject(1);
                                break;
                            default:
                                setProject(0);
                                break;

                        }

                        gradePosition = position;
                        subjectPosition = 999;


                    }
                });
            }
        });
    }


    /**
     * @param type 年级不同， 科目不同，大致分为三类   0 全部; 1 初二、初三; 2小学 ;
     */
    private void setProject(int type) {

        List<String> data = null;
        switch (type) {
            case 0:
                data = subject1;
                break;
            case 1:
                data = subject2;
                break;
            case 2:
                data = subject3;
                break;
        }
        final List<String> finalData = data;
        rcySubject.setAdapter(new CommonAdapter<String>(context, R.layout.cell_subject, data) {
            @Override
            protected void convert(final ViewHolder holder, String s, final int position) {

                if (subjectPosition == position) {
                    ((TextView) holder.getView(R.id.tv_name)).setTextColor(context.getResources().getColor(R.color.tv_tab_select_text));
                }

                holder.setText(R.id.tv_name, s);
                holder.getView(R.id.tv_name).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((TextView) holder.getView(R.id.tv_name)).setTextColor(context.getResources().getColor(R.color.tv_tab_select_text));
                        dismiss();
                        subjectPosition = position;

                        choose.Selected(grade.get(gradePosition), finalData.get(subjectPosition));
                    }
                });
            }
        });

    }







    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {


        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, 0, 0);
        } else {
            this.dismiss();

        }
    }

    public void setOnChoose(OnChoose onChoose) {
        choose = onChoose;
    }

    public interface OnChoose {

        void Selected(String grade, String subjecte);
    }
}


